package game.tileMap.model
{
	import flash.events.EventDispatcher;
	
	import game.tileMap.DataBase;
	import game.tileMap.model.vo.BossUnit;
	import game.tileMap.model.vo.Unit;
	

	public class Troop extends EventDispatcher
	{
		public var preemptive:Boolean;
		public var members:Array
		public var id:int;
		public var name:String;
		protected var db:DataBase=DataBase.getInstance();
		public function Troop()
		{
			init()
		}
		
		protected function init():void{
			trace("初始化队伍！！！")
			members=[];
			 for (var i:int;i<3;i++){
				var battler:Unit=new Battler;
				battler.through=false
				battler.y=i;
				members.push(battler);
			}
			 
			 var boss:BossUnit=new BossUnit
			boss.through=false;
			boss.x=3
			boss.y=3
			members.push(boss);
		}
		
		public function setCamp(camp:int):void{
			for (var i:int;i<members.length;i++){
				var battler:Unit=members[i];
				
				battler.camp=camp;
			}
			
		}
		
		public function getUnit(index:int):Unit{
			return members[index]
		}
		
		
		
		//获取可战斗队员
		public function  get existing_members():Array{
			var result:Array = [];
			for each (var battler:Battler in members){
				if (battler.isExist){
					result.push(battler);
				}
			}
			return result;
		}
		
		//获取无法战斗队员
		public function get dead_members():Array{
			var result:Array = [];
			for each (var battler:Battler in members){
				if (battler.isDead){
					result.push(battler);
				}
			}
			return result;
		}
		
		public function get isAllDead():Boolean{
			return false;
		}
		
		
		
		
		
		
		
		
		public function make_actions():void{
			if (preemptive){
				clear_actions();
			}else{
				for each (var monster:Battler in members){
					monster.makeAction();
				}
			}
		}
		
		public function clear_actions():void{
			for each (var battler:Battler in members){
				battler.action.clear();
			}
			
		}
		
		//清除所有队员行动
		
		
		//随机目标选择
		public function randomTarget():Battler{
			var  roulette:Array = [];
			var readyMembers:Array=existing_members;
			
			//添加敌人到轮流
			for each (var battler:Battler in readyMembers){
				roulette.push(battler); 
			}
			//转轮盘赌，决定敌人
			if (roulette.length>0){
				var bigo:int=Math.floor(Math.random()*roulette.length)
				trace(roulette.length+"轮盘中的敌人"+bigo)
				return roulette[bigo];
			}
			return null;
			
			
			
			
		}
		
		public function randomDeadTarget():Battler{
			var  roulette:Array = [];
			var readyMembers:Array=dead_members;
			
			//添加敌人到轮流
			for each (var battler:Battler in readyMembers){
				roulette.push(battler); 
			}
			//转轮盘赌，决定敌人
			if (roulette.length>0){
				var bigo:int=Math.floor(Math.random()*roulette.length)
				trace(roulette.length+"轮盘中的敌人"+bigo)
				return roulette[bigo];
			}
			return null;
			
		}
		
		
		
		/**
		 * 直接目标选择
		 * index : 位置	    
		 */
		public function smoothTarget(index:int):Battler{
			var member:Battler = members[index-1];
			if (member!=null&&member.isExist){
				return member;
			}
			return existing_members[0];
		}
		
		/**
		 * 直接目标选择（无法战斗者）
		 * index : 位置	    
		 */
		public function smooth_dead_target(index:int):Battler{
			var member:Battler = members[index];
			if (member!=null&&member.isDead){
				return member;
			}
			return dead_members[0];
		}
		
		
		/**
		 * 执行自动回复（回合结束时调用）
		 */
		public function do_auto_recovery():void{
			for each(var member:Battler in members){
				// 	 member.do_auto_recovery();
			}
			
		}
		
		/**
		 * 发动连续伤害效果,如中毒状态的时候（回合结束时调用）
		 */
		public function slip_damage_effect():void{
			for each(var member:Battler in members){
				member.slip_damage_effect();
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/**
		 *开始行动（回合开始） 
		 * 
		 */		
		public function startActs():void{
			
			for each (var unit:Unit in members){
				unit.isEnd=true;
				
			}
			
		}
		
		/**
		 *是否全部行动结束 
		 * @return 
		 * 
		 */		
		public function get allEnd():Boolean{
			for each (var unit:Unit in members){
				if (unit.isEnd==false){
					return false
				}
			}
			
			return true;
		}
		
		
		/**
		 *开始行动（回合开始） 
		 * 
		 */	
		public function resetTurn():void{
			for each (var unit:Unit in members){
				if (unit.isEnd==false){
					unit.isEnd=true;
				}
			}
		}
		
	}
}